﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.WVB.Framework;
using Microsoft.Practices.Composite.Events;
using Microsoft.WVB.Silverlight.Infrastructure;

namespace Microsoft.WVB.UI
{
    [TemplatePart(Name = RootCatVignette.MinimizeElementName, Type = typeof(Button))]
    public class RootCatVignette : CatVignetteBase
    {
        private const string MinimizeElementName = "MinimizeElement";

        private ExplorerBase _ParentControl = null;

        protected Button SearchButton = null;
        protected Button MinimizeButton = null;

        public RootCatVignette(ExplorerBase parentControl)
            : base()
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                Binding itemsBinding = new Binding("RootCategories");
                itemsBinding.Source = ViewModel;
                itemsBinding.Mode = BindingMode.OneWay;
                this.SetBinding(ExplorerBase.ItemsSourceProperty, itemsBinding);

                this._ParentControl = parentControl;
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            RoutedEventHandler minimizeHandler = null;

            MinimizeButton = base.GetTemplateChild(RootCatVignette.MinimizeElementName) as Button;
            if (MinimizeButton != null)
            {
                if (minimizeHandler == null)
                {
                    minimizeHandler = delegate(object sender, RoutedEventArgs e)
                    {
                        if (this._ParentControl.CurrentState == ExplorerState.Normal)
                            this._ParentControl.CurrentState = ExplorerState.Iconed;
                    };
                }
                MinimizeButton.Click += minimizeHandler;
            }

        }
        protected override void OnSelectItem(object p)
        {
            base.OnSelectItem(p);

            if (this._ParentControl.CurrentState == ExplorerState.Iconed)
            {
                IEventAggregator ag = IoC.GetCreate<IEventAggregator>();
                ag.GetEvent<ExplorerMinimizeEvent>().Publish(_ParentControl);

                this._ParentControl.CurrentState = ExplorerState.Normal;
            }
        }
    }
}
